const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()
const groupsCollection = db.collection('groups')

exports.main = async (event, context) => {
  const { planId, status } = event
  const wxContext = cloud.getWXContext()
  
  try {
    // 检查是否是创建者
    const plan = await groupsCollection.doc(planId).get()
    if (plan.data.creator !== wxContext.OPENID) {
      return {
        success: false,
        error: '没有权限进行此操作'
      }
    }

    // 更新状态
    await groupsCollection.doc(planId).update({
      data: {
        status,
        updateTime: new Date()
      }
    })

    return {
      success: true
    }
  } catch (err) {
    console.error(err)
    return {
      success: false,
      error: err.message
    }
  }
} 